<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>加入购物车</title>
  <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport"/>
  <meta content="yes" name="apple-mobile-web-app-capable"/>
  <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
  <meta content="telephone=no" name="format-detection"/>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="__STATIC__/plugs/layui/lay/modules/layer.js"></script>
</head>
<body>
<p id="price">{$goods.goods_price}</p>
{volist name="spec_name" id="v" key="kk"}
    <div>
        <p style="border:1px solid red;" id="spec_{$kk}">{$v.name}</p>
        {foreach name="spec_result" item="v1"}
              {if condition="$v1.spec_id eq $v.id"}
                  <input type="radio" name="spec_{$kk}"  value="{$v1.id}" onclick="get_price();">{$v1.detail}
              {/if}
        {/foreach}
    </div>
{/volist}
<p><a href="#">立即购买</a>&nbsp;&nbsp;<a href="javascript:;" onclick="addto_cart();">加入购物车</a></p>
<input type="hidden" name="spec_count" value="{$spec_count}">
<input type="hidden" name="goods_id" value="{$goods.goods_id}">
<input type="hidden" name="id[]" value="">
</body>
</html>
<script>
    /**
     *  添加到购物车
     */
    function addto_cart(){
        //商品属性数量
        var id = $('input[name="id[]"]').val();
        var spec_num = "{$spec_count}";
        var spec_combin = '';
        //商品id
        var goods_id = $('input[name=goods_id]').val();
        for(var i=1;i<=spec_num;i++){
            if($('input[name=spec_'+i+']:checked').val() == null){
                var spec_name = $('#spec_'+i).text();
                alert('请选择'+spec_name);
                return false;
            }
            if($('input[name=spec_'+i+']:checked').val() != null){
                spec_combin += $('input[name=spec_'+i+']:checked').val()+',';
            }
        }
        $.post("{:url('GoodsList/addto_cart')}",
            {id:id,goods_id:goods_id,specs:spec_combin},
            function(data){
                if(data.code == 1){
                    alert('添加购物车成功');
                }else{
                    alert(data.msg);
                }
            }
        );
    }
    /**
     *  获取价格
     */
    function get_price(){
        var spec_num = "{$spec_count}";
        var goods_id = $('input[name=goods_id]').val();
        //商品属性组合
        var spec_combin = '';
        for(var i=1;i<=spec_num;i++){
            if($('input[name=spec_'+i+']:checked').val() == null){
                return false;
            }
            if($('input[name=spec_'+i+']:checked').val() != null){
                spec_combin += $('input[name=spec_'+i+']:checked').val()+',';
            }
        }
        $.post("{:url('GoodsList/pre_calculate_price')}",
            {goods_id:goods_id,specs:spec_combin},
            function(data){
                if(data.code == 1){
                    $('#price').text(data.data.price);
                    $('input[name="id[]"]').val(data.data.price);
                }else{
                    $('input:radio[name^="spec_"]').removeAttr('checked');
                    alert(data.msg);
                }
            }
        );
    }
</script>